from os import listdir
from ml.knn.KNN import *
from read import img2vector


labels = []
test_dir = listdir('trainingDigits')

m = len(test_dir)
training_mat = zeros((m, 1024))

for i in range(m):
    file_name_str = test_dir[i]
    file_str = file_name_str.split(".")[0]
    num_str = int(file_str.split('_')[0])

    labels.append(num_str)
    training_mat[i, :] = img2vector('trainingDigits/%s' % file_name_str)

test_file_list = listdir('testDigits')
error_count = 0.0

m_test = len(test_file_list)
for i in range(m_test):
    file_name_str = test_file_list[i]
    file_str = file_name_str.split(".")[0]
    num_str = int(file_str.split("_")[0])

    vec_test = img2vector("testDigits/%s" % file_name_str)
    classifier_res = classify0(vec_test, training_mat, labels, 3)
    print("the classifier came back with: %d the real answer is: %d" %
          (classifier_res, num_str))

    if classifier_res != num_str:
        error_count += 1
print("\n the total number of errors: %d" % error_count)
print("\n the total error rate: %d" % (error_count / float(m_test)))
